package com.example.image_steganography_library_master;


import com.example.imagesteganographylibrary.Text.AsyncTaskCallback.TextDecodingCallback;
import com.example.imagesteganographylibrary.Text.ImageSteganography;
import com.example.imagesteganographylibrary.Text.TextDecoding;
import com.example.imagesteganographylibrary.Utils.ImageUtil;
import com.example.imagesteganographylibrary.Utils.Utility;
import com.example.imagesteganographylibrary.view.ProgressDialog;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.media.image.PixelMap;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

public class Decode extends Ability implements TextDecodingCallback {
    private static final int SELECT_PICTURE = 100;
    private static final int RESULT_OK = -1;
    private static final String TAG = "Decode Class";
    //Initializing the UI components
    private Text textView;
    private Text whether_encoded;
    private Image imageView;
    private TextField message;
    private TextField secret_key;
    private Uri filepath;
    //Bitmap
    private PixelMap original_image;
    private ProgressDialog progressDialog;

    /**
     * onStart
     *
     * @param intent onStart
     * */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_decode);
        textView = (Text) findComponentById(ResourceTable.Id_textView);
        imageView = (Image) findComponentById(ResourceTable.Id_imageview);
        message = (TextField) findComponentById(ResourceTable.Id_message);
        secret_key = (TextField) findComponentById(ResourceTable.Id_secret_key);
        whether_encoded = (Text) findComponentById(ResourceTable.Id_whether_decoded);
        Button choose_image_button = (Button) findComponentById(ResourceTable.Id_choose_image_button);
        Button decode_button = (Button) findComponentById(ResourceTable.Id_decode_button);
        //Choose Image Button
        choose_image_button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ImageChooser();
            }
        });

        //Decode Button
        decode_button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                whether_encoded.setText("");
                if (filepath != null) {
                    if (!Utility.isStringEmpty(secret_key.getText())) {
                        //Making the ImageSteganography object
                        ImageSteganography imageSteganography = new ImageSteganography(secret_key.getText(),
                                original_image);

                        //Making the TextDecoding object
                        TextDecoding textDecoding = new TextDecoding(Decode.this, Decode.this);
                        textDecoding.doInBackground(imageSteganography);
                    } else {
                        whether_encoded.setText("No message found");
                    }
                }
            }
        });
    }

    /**
     * onStartTextEncoding
     * */
    @Override
    public void onStartTextEncoding() {
        //Whatever you want to do by the start of textDecoding
        progressDialog = new ProgressDialog(this);
        progressDialog.showDialog("Decoding Message","Loading, Please Wait...");
    }

    /**
     * onCompleteTextEncoding
     *
     * @param result ImageSteganography
     * */
    @Override
    public void onCompleteTextEncoding(ImageSteganography result) {
        //By the end of textDecoding
        progressDialog.hideDialog();
        if (result != null) {
            if (!result.isDecoded())
                whether_encoded.setText("No message found");
            else {
                if (!result.isSecretKeyWrong()) {
                    message.setText("" + result.getMessage());
                } else {
                    whether_encoded.setText("Wrong secret key");
                }
            }
        } else {
            whether_encoded.setText("Select Image First");
        }
    }

    private void ImageChooser() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(IntentConstants.ACTION_CHOOSE)
                .build();
        intent.setType("image/*");
        intent.setOperation(operation);
        startAbilityForResult(intent, SELECT_PICTURE);
    }

    /**
     * onAbilityResult
     *
     * @param requestCode int
     * @param resultCode int
     * @param data Intent
     * */
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        //Image set to imageView
        if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK && data != null && data.getUri() != null) {
            filepath = ImageUtil.getPath(data.getUri());
            original_image = ImageUtil.getPixMap(this, filepath, 1);
            if (original_image != null) {
                imageView.setPixelMap(original_image);
            }
        }
    }
}
